package com.caowj.composedemo.composable

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

/**
 * <pre>
 *     Compose的State
 *     https://blog.csdn.net/Mr_Tony/article/details/118858756
 *     作者：Caowj
 *     邮箱：caowj1104@163.com
 *     日期：2022/3/7 16:18
 * </pre>
 */

@Composable
fun _counterTest() {
    val count = remember { mutableStateOf(0) }
    Button(onClick = { count.value++ }) {
        Text("I've been clicked ${count.value} times")
    }
}


@Composable
fun TestState() {
    Column(modifier = Modifier.padding(16.dp)) {
        var name by remember { mutableStateOf("") }

        if (name.isNotEmpty()) {
            Text(
                text = "Hello, $name!",
                modifier = Modifier.padding(bottom = 8.dp),
                style = MaterialTheme.typography.h5
            )
        }
        OutlinedTextField(
            value = name,
            onValueChange = { name = it },
            label = { Text("Name") }
        )
    }
}

@Composable
fun MyScreenContent(
    names: List<String> = listOf(
        "Android",
        "there",
        "IOS",
        "PHP",
        "JAVA",
        ".Net"
    )
) {
    val counterState = remember { mutableStateOf(0) }

    Column(modifier = Modifier
        .fillMaxWidth()
        .padding(top = 40.dp)) {
        for (name in names) {
            _Greeting(msg = name)
            Divider(color = Color.Black)
        }
        Divider(color = Color.Transparent, thickness = 32.dp)

        //计数器
        _Counter(
            count = counterState.value,
            updateCount = { newCount ->
                counterState.value = newCount
            }
        )
    }
}

@Composable
fun _Greeting(
    msg: String
) {
    Text(text = "Hello ${msg}!")
}

@Composable
fun _Counter(count: Int, updateCount: (Int) -> Unit) {
    Button(onClick = { updateCount(count + 1) }) {
        Text("I've been clicked $count times")
    }
}
